layui.use(['layer', 'form', 'table', 'ztree', 'laydate', 'admin', 'ax'], function () {
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var laydate = layui.laydate;
    var admin = layui.admin;
    var imagePath = "https://exam-1257801634.cos.ap-chengdu.myqcloud.com/";
    /**
     * 系统管理--用户管理
     */
    var exam = {
        tableId: "examTable",    //表格id
        condition: {
            name: "",
            deptId: "",
            timeLimit: ""
        }
    };

    /**
     * 初始化表格的列
     */
    exam.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'exam_result_user_id', hide: true, sort: true, title: 'exam_result_user_id'},
            {field: 'student_code', sort: true, title: '考生号'},
            {field: 'name', sort: true, title: '姓名'},
            {field: 'name', sort: true, title: '准考证'},
            {field: 'full_name', sort: true, title: '所在班级'},
            {field: 'school_name', sort: true, title: '学校名称'},
            {field: 'exam_title', sort: true, title: '考试名称'},
            {field: 'exam_text', sort: true, title: '考试内容'},
            {field: 'exam_time', sort: true, title: '时间'},
            {field: 'exam_result', sort: true, title: '总成绩'},
            {field: 'exam_work', sort: true, title: '作品图',height: 100,width: 150,templet: function (d) {
                    if(d.exam_work){
                        var img ="";
                        d.exam_work.split(",").forEach(work=>{
                            img += "<div><img src='"+imagePath+work+"'></div>"
                        })
                    }
                    return img;
                }},
            {field: 'remarks', sort: true, title: '备注'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 150}
        ]];
    };

    /**
     * 点击查询按钮
     */
    exam.search = function () {
        var queryData = {};
        queryData['name'] = $("#name").val();
        table.reload(exam.tableId, {where: queryData});
    };

    /**
     * 弹出添加用户对话框
     */
    exam.openAddUser = function () {
        admin.putTempData('formOk', false);
        layer.full(layui.admin.open({
            type: 2,
            title: '添加',
            area: ['100%', '100%'],
            content: Feng.ctxPath + '/exam/examResult_add',
            end: function () {
                admin.getTempData('formOk') && table.reload(exam.tableId);
            }
        }));
    };

    /**
     * 导出excel按钮
     */
    exam.exportExcel = function () {
        var checkRows = table.checkStatus(exam.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑用户按钮时
     *
     * @param data 点击按钮时候的行数据
     */
    exam.onEditExam = function (data) {
        admin.putTempData('formOk', false);
        layer.full(layui.admin.open({
            type: 2,
            title: '编辑',
            area: ['100%', '100%'],
            content: Feng.ctxPath + '/exam/examResult_edit?student_code=' + data.student_code
                + "&examResultUserId="+data.exam_result_user_id,
            end: function () {
                admin.getTempData('formOk') && table.reload(exam.tableId);
            }
        }));
    };
    exam.onDeleteExam = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/exam/examResulDel", function () {
                table.reload(exam.tableId);
                Feng.success("删除成功!");
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("examResultUserId", data.exam_result_user_id);
            ajax.start();
        };
        Feng.confirm("是否删除" + data.name + data.exam_text + "?", operation);
    }

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + exam.tableId,
        url: Feng.ctxPath + '/exam/examResultList',
        page: true,
        height: "full-98",
        cellMinWidth: 120,
        cols: exam.initColumn()
    });


    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        exam.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        exam.openAddUser();
    });

    // 导出excel
    $('#btnExp').click(function () {
        exam.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + exam.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            exam.onEditExam(data);
        }else if (layEvent === 'delete') {
            exam.onDeleteExam(data);
        }
    });

});
